package org.tlipoca.smsteamstore.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.result.PageResult;
import org.genntii.smcommon.result.Result;
import org.genntii.smdomain.entity.SteamAppDetail;
import org.genntii.smdomain.entity.SteamStore;
import org.genntii.smdomain.vo.SteamStoreDocVO;
import org.genntii.smdomain.vo.SteamStoreVO;
import org.springframework.web.bind.annotation.*;
import org.tlipoca.smsteamstore.service.EsService;
import org.tlipoca.smsteamstore.service.SteamStoreCacheService;
import org.tlipoca.smsteamstore.service.SteamStoreService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

@Tag(name = "steam商店")
@Slf4j
@RestController
@RequestMapping("/steam_store")
public class SteamStoreController {

    @Autowired
    public SteamStoreService steamStoreService;

    @Resource
    private SteamStoreCacheService steamStoreCacheService;

    @Resource
    private EsService esService;

    @GetMapping("/recommend_home")
    public Result<ArrayList<SteamStoreVO>> getRecommendHome(){
        return Result.success(steamStoreCacheService.getRecommendHome());
    }

    @Operation(summary = "每日推荐轮播图")
    @GetMapping("/recommend_daily")
    public Result<ArrayList<SteamAppDetail>> getRecommendDaily(){
        return Result.success(steamStoreCacheService.getRecommend());
    }

    //TODO. 使用ElasticSearch搜索Steam_Store内容
    //TODO Test
    @GetMapping("/search")
    @Operation(summary = "steam库存搜索")
    public Result<PageResult<SteamStoreDocVO>> searchSteamStore(@RequestParam String keyword,
                                                                @RequestParam Integer pageNum,
                                                                @RequestParam Integer pageSize) {
        return Result.success(esService.search(keyword,pageNum,pageSize));
    }

    //TODO 启用springCache并测试
    @GetMapping("/{appid}")
    @Operation(summary = "根据Appid获取Steam库存")
    public Result<SteamAppDetail> getSteamStoreById(@PathVariable Integer appid) {
        return Result.success(steamStoreService.getSteamStoreByIdService(appid));
    }

    @GetMapping("/list")
    @Operation(summary = "获取SteamAppid列表")
    public Result<ArrayList<Integer>> getSteamStoreAppidList() {
        return Result.success(steamStoreService.getSteamStoreAppidListService());
    }

    @Operation(summary = "SteamStore是否存在")
    @GetMapping("/exists/{appid}")
    public Result<Boolean> steamStoreExists(@PathVariable Integer appid) {
        return Result.success(steamStoreService.steamStoreExistsService(appid));
    }

    @GetMapping("/listBatch")
    @Operation(summary = "根据ids批量获取库存信息")
    public Result<ArrayList<SteamStore>> getSteamStoreBatchByIds(@RequestParam("ids") ArrayList<Integer> steamStoreIds){
        return Result.success(steamStoreService.getSteamAppListBatchByIds(steamStoreIds));
    }

}
